﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;

namespace PeisongWeb.admin
{
    public partial class partnerEdit : adminPage
    {
        protected int id
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["id"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["id"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("AE", 1))
                {
                    if (Request.QueryString["category"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["category"]))
                        {
                            id = Convert.ToInt32(Request.QueryString["category"]);
                        }
                    }
                    if (id != 0)
                    {
                        getDetails();
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        private void getDetails()
        {
            DAL.PartnerEntity category = logic.partner.getById(id);
            txtTitle.Text = category.Title;
            txtDesc.Text = category.Description;
            txtURL.Text = category.Url;
            txt_Sort.Text = category.Sort.ToString();
            chx_isShow.Checked = category.IsShow;
            txtAbs.Text = category.Abbr;
            if (category.ImgName != null)
            {
                imgCurrent.Visible = true;
                imgCurrent.ImageUrl = Library.Resource.UploadPath + Library.Resource.FileOriginal + "/" + category.ImgName;
                imgCurrent.AlternateText = "";
                hdCurrentImageFileName.Value = category.ImgName;
            }
            else
            {
                imgCurrent.Visible = false;
            }
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (imgName.HasFile)
            {
                if (!Library.FileLib.isPicByFileName(imgName.PostedFile.FileName))
                {
                    Library.Script.ClientMsg("无效的图片，请重新选择.");
                    return;
                }
            }

            PartnerEntity cat = new PartnerEntity();
            cat.Title = txtTitle.Text;
            cat.Abbr = txtAbs.Text;
            cat.Description = txtDesc.Text;
            int sort = 0;
            int.TryParse(txt_Sort.Text, out sort);
            cat.Sort = sort;
            cat.IsShow = chx_isShow.Checked;
            cat.AdminId = logic.sysAdmin.AdminID;
            cat.Url = txtURL.Text;
            if (id != 0)
            {
                if (imgName.HasFile)
                {
                    if (hdCurrentImageFileName.Value != "")
                    {
                        try
                        {
                            Library.FileLib.Del(Library.Resource.UploadPath + Library.Resource.FileOriginal + "/" + hdCurrentImageFileName.Value);
                        }
                        catch
                        {
                        }
                    }
                    cat.ImgName = Library.ImgLib.getUploadFilenameNotMakeThumb(imgName.PostedFile);
                }
                else
                {
                    cat.ImgName = hdCurrentImageFileName.Value;
                }

                cat.Pid = id;
                logic.partner.update(cat);
            }
            else
            {
                if (imgName.HasFile)
                {
                    cat.ImgName = Library.ImgLib.getUploadFilenameNotMakeThumb(imgName.PostedFile);
                }
                logic.partner.insert(cat);

            }

            Library.Script.ClientMsgUrl("保存成功！", "partnerList.aspx");

        }
    }
}
